package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ResultCode;
import com.example.exception.BusinessException;
import com.example.mapper.ProductFeatureMapper;
import com.example.mapper.ProductMapper;
import com.example.model.dto.ProductQueryDTO;
import com.example.model.entity.Product;
import com.example.model.entity.ProductFeature;
import com.example.model.vo.PageResult;
import com.example.model.vo.ProductVO;
import com.example.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品服务实现类
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    
    private final ProductFeatureMapper productFeatureMapper;
    
    public ProductServiceImpl(ProductFeatureMapper productFeatureMapper) {
        this.productFeatureMapper = productFeatureMapper;
    }
    
    @Override
    public PageResult<ProductVO> listProducts(ProductQueryDTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(queryDTO.getCategory())) {
            queryWrapper.eq(Product::getCategory, queryDTO.getCategory());
        }
        queryWrapper.orderByDesc(Product::getCreatedAt);
        
        // 分页查询
        Page<Product> page = new Page<>(queryDTO.getPage(), queryDTO.getPageSize());
        Page<Product> productPage = page(page, queryWrapper);
        
        // 转换结果
        List<ProductVO> productVOList = productPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
        
        return PageResult.of(
                productVOList,
                productPage.getTotal(),
                queryDTO.getPage(),
                queryDTO.getPageSize()
        );
    }
    
    @Override
    public ProductVO getProductById(Long id) {
        Product product = getById(id);
        if (product == null) {
            throw new BusinessException(ResultCode.NOT_FOUND, "产品不存在");
        }
        return convertToVO(product);
    }
    
    /**
     * 将产品实体转换为VO对象
     */
    private ProductVO convertToVO(Product product) {
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        
        // 查询产品特性
        LambdaQueryWrapper<ProductFeature> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductFeature::getProductId, product.getId());
        List<ProductFeature> features = productFeatureMapper.selectList(queryWrapper);
        
        // 设置特性列表
        List<String> featureNames = features.stream()
                .map(ProductFeature::getFeatureName)
                .collect(Collectors.toList());
        productVO.setFeatures(featureNames);
        
        return productVO;
    }
} 